/* 菜谱页面路由 */
// 引入 express
const express = require('express');
// 创建路由器
const r = express.Router();
// 引入连接池
const pool = require('../../model/mysql.js');

// 1. 获取菜系  (get /v1/cuisines/recipe)
//  http://127.0.0.1:8888/v1/cuisines/recipe
r.get('/recipe', (req, res, next) => {
  pool.query('select * from cuisine where ctype="菜系"', (err, result) => {
    if (err) {
      next(err);
      return;
    }
    res.send({ code: 200, msg: '查询成功', data: result })
  });
});

// 2. 查询对应菜品 (get /v1/cuisines/cuisine/:cid)
// http://127.0.0.1:8888/v1/cuisines/cuisine/1
// 菜品名 图片 配料名   cid 菜系id    rid 菜品id
r.get('/cuisine/:cid', (req, res, next) => {
  pool.query('select * from recipe where sub_id=?', [req.params.cid], (err, result) => {
    if (err) {
      next(err)
      return
    }
    res.send({ code: 200, msg: '查询成功', data: result })
  })
})

// 3. 获取全部专题 + 子分类 (get /v1/cuisines/topic)
//  http://127.0.0.1:8888/v1/cuisines/topics
r.get('/topics', (req, res, next) => {
  pool.query('select * from topic inner join cuisine on tid=cid', (err, result) => {
    if (err) {
      next(err)
      return
    }
    res.send({ code: 200, msg: '查询成功', data: result })
  })
})

// 4. 查询单个专题 (get /topic/:tid)
// http://127.0.0.1:8888/v1/cuisines/topic/9
r.get('/topic/:tid', (req, res, next) => {
  console.log(req.params);
  pool.query('select * from topic where ?', [req.params], (err, result) => {
    if (err) {
      next(err)
      return
    }
    res.send({ code: 200, msg: '查询成功', data: result })
  })
})

// 搜索页面  菜谱搜索 (get  /search/:rname)
// http://127.0.0.1:8888/v1/cuisines/search/麻辣
r.get('/search/:rname', (req, res, next) => {
  console.log(req.params.rname);
  pool.query('select * from recipe where rec_name like ? ', [`%${req.params.rname}%`], (err, result) => {
    if (err) {
      next(err)
      return
    }
    res.send({ code: 200, msg: '查询成功', data: result })
  })
})
r.get('/square', (req, res, next) => {
  pool.query('select * from seller', (err, result) => {
    if (err) {
      next(err)
      return
    }
    res.send({ code: 200, msg: '查询成功', data: result })
  })
})
r.get('/shop', (req, res, next) => {
  // console.log(req);
  let id = req.query.id
  console.log(id);
  pool.query('select * from seller where id=?', [id], (err, result) => {
    if (err) {
      next(err)
      return
    }
    res.send({ code: 200, msg: '查询成功', data: result })
  })
})
r.get('/pingfen', (req, res, next) => {
  // console.log(req);
  let id = req.query.id
  let pingfen = req.query.pingfen
  console.log(pingfen);
  pool.query('update seller set pingfen=? where id=? ', [pingfen, id], (err, result) => {
    if (err) {
      next(err)
      return
    }
    res.send({ code: 200, msg: '查询成功', data: result })
  })
})
r.get('/curColor', (req, res, next) => {
  // console.log(req);
  let id = req.query.id
  let curColor = '#' + req.query.curColor
  console.log(req.query);
  pool.query('update seller set curColor=? where id=? ', [curColor, id], (err, result) => {
    if (err) {
      next(err)
      return
    }
    res.send({ code: 200, msg: '查询成功', data: result })
  })
})
r.get('/quanzi',(req,res,next)=>{
  pool.query('select * from quanzi',(err,res1)=>{
    if(err){
      next(err)
      return
    }
    // console.log(res1);
    let arr=[]
    res1.forEach((item,index)=>{
        pool.query('select imgs from imgs where pageId =?',item.pageId,(err,res2)=>{
          if(err){
            next(err)
            return
          }else{
            // console.log(res2);
            arr.push(res2)
           
            if(index==res1.length-1){
              console.log(arr);
              res.send({code:200,msg:'成功',data:{arr,res1}})
              // arr.forEach(i=>{
              //   // console.log(i);
              //   // res1.push(i)
              // })
            }
          }
          
        })
       
       })
  })
})




module.exports = r